package com.yb;

import java.util.Iterator;
import java.util.ServiceLoader;

public class OADemo8 {
    public static void main(String[] args) throws Exception {
        Double salary = 15000.00;

        //使用 SalaryJARLoader6，就需要在 OADemo 中添加 SPI 的配置文件
        while (true) {
            SalaryJARLoader6 salaryJARLoader = new SalaryJARLoader6("/Users/yinbo/Desktop/ybWork/jvm-related/OA_Sale/OA_Sale.jar");
            ISale service = getSalaryService(salaryJARLoader);
            System.out.println("应该到手Money:" + service.salePrice(salary));
            Thread.sleep(5000);
        }
    }
    private static ISale getSalaryService(ClassLoader classloader) {
        ISale service = null;
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(classloader);
            ServiceLoader<ISale> services = ServiceLoader.load(ISale.class, classloader);
            Iterator<ISale> iterator = services.iterator();
            if (iterator.hasNext()) {
                service = iterator.next();
            }
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
        return service;
    }
}

